package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.service.SystemDictionaryService;
import com.xmg.p2p.base.service.UserinfoService;
import com.xmg.p2p.base.util.JSONResult;
import com.xmg.p2p.base.util.RequireLogin;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.p2p.base.controller
 * @author: Andy
 * @date: 2021.3.19 14:35
 * @Description: 个人资料控制器
 * @since JDK 1.8
 */
@Controller
public class BasicInfoController {

    @Autowired
    private SystemDictionaryService systemDictionaryService;
    @Autowired
    private UserinfoService userinfoService;

    /**
     * 打开个人中心页面
     * @param model
     * @return
     */
    @RequireLogin
    @RequestMapping("basicInfo")
    public String basicInfo(Model model){
        // 当前用户
        Logininfo current = UserContext.getCurrent();
        //  添加所有下拉列表相关的内容
        model.addAttribute("userinfo",this.userinfoService.getById(current.getId()));
        model.addAttribute("educationBackgrounds",this.systemDictionaryService.selectByParentSn("educationBackground"));
        model.addAttribute("incomeGrades",this.systemDictionaryService.selectByParentSn("incomeGrade"));
        model.addAttribute("marriages",this.systemDictionaryService.selectByParentSn("marriage"));
        model.addAttribute("kidCounts",this.systemDictionaryService.selectByParentSn("kidCount"));
        model.addAttribute("houseConditions",this.systemDictionaryService.selectByParentSn("houseCondition"));
        return "userinfo";
    }

    /**
     * 保存基本信息
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("basicInfo_save")
    public JSONResult basicInfo_save(Userinfo userinfo){
        JSONResult jsonResult = new JSONResult();
        try {
            // 更新用户基本信息
            this.userinfoService.updateBasicInfo(userinfo);
        }catch (Exception ex){
            jsonResult.setMsg("错误");
            jsonResult.setSuccess(false);
            ex.printStackTrace();
        }
        return jsonResult;
    }
}
